﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace SharingCenter.Web.Admin.User
{
    using SharingCenter.Web.Classes;
    using SharingCenter.Web.Account;
    using System.Reflection;
    using SharingCenter.Web.Classes.Profiles;
    public partial class Default : System.Web.UI.Page
    {
        protected void Page_Init(object sender, EventArgs e)
        {
            LinqDataSource1.AutoPage = true;
            LinqDataSource1.Selecting += new EventHandler<LinqDataSourceSelectEventArgs>(LinqDataSource1_Selecting);
            LinqDataSource1.Selected += new EventHandler<LinqDataSourceStatusEventArgs>(LinqDataSource1_Selected);
            //GridView1.RowCommand += new GridViewCommandEventHandler(GridView1_RowCommand);
            //GridView1.RowDataBound += new GridViewRowEventHandler(GridView1_RowDataBound);
            lvUsers.ItemCommand += new EventHandler<ListViewCommandEventArgs>(lvUsers_ItemCommand);
            lvUsers.ItemDataBound += new EventHandler<ListViewItemEventArgs>(lvUsers_ItemDataBound);
            //LinqDataSource2.Selecting += new EventHandler<LinqDataSourceSelectEventArgs>(LinqDataSource2_Selecting);
        }

        void LinqDataSource1_Selecting(object sender, LinqDataSourceSelectEventArgs e)
        {
            if ("1" == Request.QueryString["s"])
            {
                e.Result = DoSearch();
            }
        }

        protected void Page_Load(object sender, EventArgs e)
        {
 
        }

        private List<ProfileMembership> DoSearch()
        {
            using (scDataContext cxt = new scDataContext())
            {
                string query = string.Format("EXEC SearchUsers '', '{0}'", Request.QueryString["nn"]);
                return cxt.ExecuteQuery<ProfileMembership>(query).ToList();
            }
        }

        void LinqDataSource2_Selecting(object sender, LinqDataSourceSelectEventArgs e)
        {
            
                if (!IsPostBack)
                {
                    e.Cancel = true;
                }
                else
                {
                    e.Result = ProfileOperations.GetProfile((Guid)ViewState["UID"]);
                }
            
        }

        void lvUsers_ItemDataBound(object sender, ListViewItemEventArgs e)
        {
            try
            {
                Guid uid = (Guid)lvUsers.DataKeys[((ListViewDataItem)e.Item).DataItemIndex].Value;
                Label l = (Label)e.Item.FindControl("lblNickName");
                try
                {
                    l.Text = ProfileOperations.GetProfile(uid).NickName;
                }
                catch { l.Text = ""; }
            }
            catch { }
        }

        void lvUsers_ItemCommand(object sender, ListViewCommandEventArgs e)
        {
            switch (e.CommandName)
            {
                case "LockUser":
                    DoLockUser(ref e);
                    UpdatePanel1.Update();
                    break;
                case "UnlockUser":
                    DoUnlockUser(ref e);
                    UpdatePanel1.Update();
                    break;
                case "DeleteUser":
                    DoDeleteUser(ref e);
                    Response.Redirect(Request.Url.PathAndQuery);
                    //UpdatePanel1.Update();
                    break;
                case "EditProfile":
                    DoEdit(ref e);
                    //UpdatePanel2.Update();
                    break;
                default: break;
            }
            lvUsers.DataBind();
        }

        private void DoEdit(ref ListViewCommandEventArgs e)
        {
            ViewState["UID"] = (Guid)lvUsers.DataKeys[((ListViewDataItem)e.Item).DataItemIndex].Value;
            //FormView1.DataBind();
            UpdatePanel1.Update();
        }

        //void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
        //{
        //    Guid uid = (Guid)GridView1.DataKeys[e.Row.DataItemIndex].Value;
        //    Label l = (Label)e.Row.FindControl("lblNickName");
        //    try
        //    {
        //        l.Text = ProfileOperations.GetProfile(uid).NickName;
        //    }
        //    catch { l.Text = ""; }
        //}

        void GridView1_RowCommand(object sender, GridViewCommandEventArgs e)
        {
            
        }

        private void DoDeleteUser(ref ListViewCommandEventArgs e)
        {
            Guid uid = new Guid(e.CommandArgument.ToString());
            Classes.Account.AccountOperations.CancelAccount(uid);

        }

        private void DoUnlockUser(ref ListViewCommandEventArgs e)
        {
             Guid uid = new Guid(e.CommandArgument.ToString());
             Classes.Account.AccountOperations.UnlockUser(uid);
        }

        private void DoLockUser(ref ListViewCommandEventArgs e)
        {
            Guid uid = new Guid(e.CommandArgument.ToString());
            Classes.Account.AccountOperations.LockUser(uid, "");
        }

        void LinqDataSource1_Selected(object sender, LinqDataSourceStatusEventArgs e)
        {
            lblUserCount.Text = e.TotalRowCount.ToString();
        }

        void GridView1_DataBound(object sender, EventArgs e)
        {
            
        }
    }
}
